import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))

const displayData = [{ name:'显示', value:'1' },{ name:'不显示', value:'2' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 提交表单列表
		formData : [
			{ label: '字段名称',		name: 'label_field', 			half:true, required:true },
			{ label: '导出名称',		name: 'label_name', 			half:true, required:true },
			{ label: '排序',			name: 'sort', 					half:true,	type:'number' },
			{ label: '导出是否显示',	name: 'display', 				half:true, 	type:'select', data:displayData },
			{ label: '导出列宽',		name: 'label_width', 			half:true,	type:'number' },
			{ label: '处理方式',		name: 'label_format_type', 		half:true, 	type:'select', nameStr:'label', br:true, onValue:({value, formData})=>{
				$fn.disabledRows(formData,'label_format_value', +value === 0)
				this.setState({ formData })
			} },
			{ label: '',			name: 'label_format_value', 	half:true,  style:{paddingLeft:10}, disabled:true },
		]
	}
	
	model = {}
	resetModel = { }
	
	componentDidMount(){
		const { model_code } = $fn.query()
		this.submit = { model_code  }
		if(model_code){ 
			this.query = { model_code }
			this.resetModel = { model_code }
			this.model = $fn.copy(this.resetModel)
			this.model_code = model_code
		}
		
		this.fetch()
	}
	
	// 获取弹框下拉
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'label_format_type' ], ()=>{
			$cache.then( async f => {
				// 处理方式
				$fn.setRow(formData, 'label_format_type', await f.getCache({ api:'labelFormatType' } ) )
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'settings/getExportModelConfig', { paging:'get'})
	
	// 表单
	cols = [
		{ title: '字段名称', 		dataIndex: 'label_field', 				width:150 },
		{ title: '导出名称', 		dataIndex: 'label_name', 				width:150 },
		{ title: '处理方式', 		dataIndex: 'label_format_type_name', 	width:150 },
		{ title: '排序', 			dataIndex: 'sort', 						width:150 },
		{ title: '导出是否显示', 		dataIndex: 'display', 					width:150, render: text=> +text === 1 ? '显示':'不显示' },
		{ title: '导出列宽', 		dataIndex: 'label_width', 				width:150 },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text, rows) => {
		const { uuid } = rows
		return [
			TableControls.edit.call(this, null,{ uuid }, { rows, select:this.getModalSelect }),				// 编辑
			this.onDel([ uuid ]),																						// 删除
		]
	}
	
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this, { click: this.getModalSelect }),				// 添加
			this.onDel(),															// 删除
			Controls.back.call(this),												// 返回
		]
	}
	
	// 删除
	onDel = uuid => Delete.call(this, 'settings/delExportModelConfig', { uuid, isKeys: !uuid, idStr:'uuids' })
	
	render(){
		return (
			<>
				<Page title='配置导出模板' controls={this.controls()}>
					{/* 表格 */}
					{Table.call(this)}
				</Page>
				{/* 添加 */}
				{ AddEditModal.call(this,{ labelWidth:'s7', width:800, api: 'settings/addExportModelConfig',editApi: 'settings/editExportModelConfig'}) }
			</>
		)
	}
}

export default Index